package com.hunk.core.auth.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hunk.core.Constants;
import com.hunk.core.annotation.ActionTrace;
import com.hunk.core.auth.entity.User;
import com.hunk.core.auth.service.UserService;
import com.hunk.springMvcExercise.web.Response;
import com.hunk.utils.exception.CustomizeException;

/**返回为jsp的controller类
 * @author HunkDeng
 *
 */
@Controller
@RequestMapping("/user")
public class LoginConroller {
	@Autowired
	UserService userService;
	
	private static Logger logger = Logger.getLogger(LoginConroller.class);
	/**返回jsp页面
	 * @return
	 */
	@RequestMapping(value="/index")
	public String index(HttpServletRequest request,HttpServletResponse responsse) {
		logger.info("com.hunk.springMvcExercise.web.controller.UserConroller.index(req,res)");
		return "user/index";
	}
	
	/**跳转已定义的tile
	 * @return
	 */
	@RequestMapping(value="/easyRedirect/{path}")
	public String easyRedirect(HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			@PathVariable("path") String path) {
//		if (null != session.getAttribute(Constants.LOGIN_USERNAME.name())) {
//			return "homeView";
//		}
		return path;
	}
	
	/**跳转已定义的tile
	 * @return
	 */
	@RequestMapping(value="/guestLogin")
	public ModelAndView guestRedirect(HttpServletRequest request,
			HttpServletResponse response,
			HttpSession session) {
		return loginHandler(new User("Guest", null), session);
	}

	/**处理form表单并返回success页面或failed页面
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/login",method=RequestMethod.POST)
	public ModelAndView login(@ModelAttribute("user")User user, HttpSession session) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("user/login_failed");		
		try {
			User _user = userService.checkUser(user);
			return loginHandler(_user,session);
		} catch (CustomizeException e) {
			mav.addObject("errorMsg", e.getMessage());
		}
		return mav;
	}
	
	private ModelAndView loginHandler(User user, HttpSession session) {
		session.setAttribute(Constants.LOGIN_USERNAME.name(), user.getUsername());
		ModelAndView mav = null;
		if (user.isEnabled()) {
			mav = new ModelAndView(new RedirectView("easyRedirect/userManagementView"));
		} else {
			mav = new ModelAndView(new RedirectView("easyRedirect/homeView"));
		}
//		mav.addObject(user);
//		mav.getModelMap().put("content", "content1");
		return mav;
	}
	
	public ModelAndView loginOut() {
		return null;
	}
	
	/** 测试requestparam的入参是否检查
	 *  并查看错误信息
	 * @param id
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/login2",method=RequestMethod.POST)
	public ModelAndView login2(
				@RequestParam(value="id",required=true)String id,
				@ModelAttribute("user")User user) {
		if ("hunk".equals(user.getUsername())) {
			if ("hunk".equals(user.getPassword())) {
				ModelAndView mav = new ModelAndView();
				mav.setViewName("user/login_success");
				mav.addObject(user);
				return mav;
			}
		}
		ModelAndView mav = new ModelAndView();
		mav.setViewName("user/login_failed");
		return mav;
	}
	
	@ActionTrace
	@RequestMapping(value="/showUserInManyWays")
	public String showUserInManyWays(HttpSession session, ModelMap mm) {
		User user = new User();
		user.setUsername("hunk");
		user.setPassword("psd");
		
		
		User user2 = new User();
		user2.setUsername("hunk2");
		user2.setPassword("psd");
		
		Response res = new Response();
		res.getList().add(user);
		res.getList().add(user2);
		mm.addAttribute("baseResponse", res);
		return "user/showUserInManyWays";
	}
}
